import hashlib


def cal_md5(file_content):
	file_hash = hashlib.md5()

	if isinstance(file_content, str):
		file_content = file_content.encode('utf-8')
	file_hash.update(file_content)
	return file_hash.hexdigest()


# 使用流式MD5计算，防止大文件会内存溢出
async def calc_stream_md5(upload_file):
	hash_md5 = hashlib.md5()
	while chunk := await upload_file.read(8192):
		hash_md5.update(chunk)
	await upload_file.seek(0)  # 重置文件指针
	return hash_md5.hexdigest()
